package com.itxiaoxiong.chat.mapper;

import com.itxiaoxiong.chat.pojo.ChatMessage;
import com.itxiaoxiong.chat.pojo.FriendRelationship;
import com.itxiaoxiong.chat.pojo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface FriendMapper {

    /*
    根据username查询好友
     */
    @Select(("select * from user where username = #{username}"))
    User listName(@Param("username") String username);

    /*
    添加好友
     */
    @Insert("insert into friend_relationship(user_id1, user_id2, status) values (#{userId1}, #{userId2}, #{status})")
    void insert(FriendRelationship friendAdd);

    /*
    检查是否存在好友关系
    */
    @Select("SELECT COUNT(*) FROM friend_relationship WHERE ((user_id1 = #{userId1} AND user_id2 = #{userId2}) OR (user_id1 = #{userId2} AND user_id2 = #{userId1}))")
    boolean checkIfFriendExists(@Param("userId1") Integer userId1,@Param("userId2") Integer userId2);


    /*
    删除好友
    */
    @Delete("DELETE FROM friend_relationship WHERE ((user_id1 = #{userId1} AND user_id2 = #{userId2}) OR (user_id1 = #{userId2} AND user_id2 = #{userId1}))")
    void delete(FriendRelationship friendDelete);


    /*
    查找好友
     */
    @Select("SELECT * FROM friend_relationship WHERE (user_id1 = #{userId} OR user_id2 = #{userId}) AND status = '1'")
    List<FriendRelationship> getFriendsByUserId(@Param("userId") Integer userId);


    /*
    查询聊天记录
     */
/*    @Select("select * from chat_message where (sender_id = #{userId1} and receiver_id = #{userId2})")*/
    @Select("select * from chat_message where ((sender_id = #{userId1} and receiver_id = #{userId2}) or (sender_id = #{userId2} and receiver_id = #{userId1}))")
    List<ChatMessage> getMessages(@Param("userId1") Integer userId1,@Param("userId2") Integer userId2);

    /*
    发消息
     */
    @Insert("insert into chat_message (sender_id, receiver_id, message_content) values (#{senderId}, #{receiverId}, #{messageContent} )")
    void addMessage(ChatMessage chatMessage);

    /*
    删除聊天记录
     */
    @Delete("delete from chat_message where((sender_id = #{senderId} and receiver_id = #{receiverId}) or (sender_id = #{receiverId} and receiver_id = #{senderId}))")
    void deleteMessage(ChatMessage chatMessage);

    /*
    发送图片
     */
    @Insert("insert into chat_message (sender_id, receiver_id, message_content, image_content) values (#{senderId}, #{receiverId}, #{messageContent} , #{imageContent})")
    void addFile(ChatMessage chatMessage);


}
